import WXP from './minapp-request/wxp.min.js'
import requestIntercept from './minapp-request/requestIntercept.js'
import util from './util.js'

const apiurl = 'http://api.hzgxjj.online/api/';
const host = 'http://api.hzgxjj.online';
const image_url = 'http://api.hzgxjj.online';
// 注册请求拦截器
WXP.intercept('request', requestIntercept);

const $ajax = (
    {    
        url = '',
        data = {},
        methods = 'POST',
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        loading = '加载中...'
    },
    {
        success = (res) =>{
            //操作成功默认方法
            util.success(res.msg);
        },
        fail = (res) =>{
            if(util.isString(res)) {
                util.error(res);
            } else {
                if(util.isUndefined(res.msg)) {
                    util.error('未知错误');
                } else {
                    util.error(res.msg);      
                }
            }
        },
        complete =  (res) =>{
            //停止下拉刷新
            wx.hideNavigationBarLoading();
        }, 
    } = {}, //默认为空对象
) => {
    // 增强体验：加载中
    wx.showNavigationBarLoading();
    if (loading && loading !== false) {
        //加载提示
        WXP.showLoading({
            title: loading,
            icon: 'none'
        })
    }
    let userinfo = getUserInfo();
  
    let token = '';
    if(!util.isUndefined(userinfo) && !util.isUndefined(userinfo.token) ) {
        token = userinfo.token;
    }
    // 构造请求体
    let request = {
        //判断是否带有http头
        url: url.indexOf(apiurl) > -1 ? url : apiurl + url,
        method: methods,
        header: Object.assign({
            'TOKEN': token
            // set something global
        }, headers),
        data: Object.assign({
            
        }, data)
    }
     
    WXP.request(Object.assign(request)).then(resp => {
        //大于0代表成功  200
        if(resp.data.code === 1) {
            success(resp.data) 
        } else {
            fail(resp.data)
        }
        
        complete(resp.data);
    }).catch(errorMesg => {
        fail(errorMesg)
        complete(errorMesg);
    })
}

const complete = function(that, res, app, page = 15) {
    let tip = '';
    if (app.util.isString(res)) {
        app.info(res);
        tip = '上拉加载';
        that.data.is_pull = true;
    } else if (!app.util.isUndefined(res.data) && app.util.isArray(res.data.data)) {
        if (res.data.data.length < page) {
            that.data.is_pull = false;
            tip = '没有更多数据'
        } else {
            tip = '上拉加载';
            that.data.is_pull = true;
        }
    }
    that.setData({
        loading: false,
        tip: tip,
    })
}

const saveUser = function(user) {
    let userinfo = JSON.stringify(user);
    wx.setStorage({
        key:"userinfo",
        data:userinfo
    })
}
const getUserInfo= function() {
    try {
        var value = wx.getStorageSync('userinfo')
        return JSON.parse(value);
      } catch (e) {
        
      }
}
module.exports = { 
    $ajax: $ajax,
    apiurl: apiurl,
    host: host,
    image_url: image_url,
    WXP: WXP, 
    complete,
    saveUser,
    getUserInfo
}

